package palmtop.com.palmtop.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;

import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Poi;
import com.amap.api.navi.AmapNaviPage;
import com.amap.api.navi.AmapNaviParams;
import com.amap.api.navi.AmapNaviType;
import com.amap.api.navi.INaviInfoCallback;
import com.amap.api.navi.model.AMapNaviLocation;
import com.beardedhen.androidbootstrap.AwesomeTextView;
import com.beardedhen.androidbootstrap.BootstrapButton;
import com.google.gson.Gson;

import net.grandcentrix.tray.AppPreferences;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;

import java.util.ArrayList;

import palmtop.com.palmtop.R;
import palmtop.com.palmtop.activity.MytaskdetailActivity;
import palmtop.com.palmtop.model.Processtask;
import palmtop.com.palmtop.model.Task;
import palmtop.com.palmtop.model.Taskgrid;
import palmtop.com.palmtop.util.Config;
import palmtop.com.palmtop.util.NetUtil;

/**
 * Created by Administrator on 2017/9/1.
 */

public class MytaskAdapter extends BaseAdapter {
    private ArrayList<Taskgrid> arrlist;
    private Context context;

    String userid = "";

    public MytaskAdapter(ArrayList<Taskgrid> arrlist, Context context) {
        this.arrlist = arrlist;
        this.context = context;
        AppPreferences appPreferences = new AppPreferences(context);
        userid = appPreferences.getString("userid","");
    }

    @Override
    public int getCount() {
        return arrlist.size();
    }

    @Override
    public Object getItem(int i) {
        return arrlist.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int pos, View view, ViewGroup viewGroup) {
        view = LayoutInflater.from(context).inflate(R.layout.item_mytask,null);
        AwesomeTextView taskname = view.findViewById(R.id.taskname);
        final AwesomeTextView status = view.findViewById(R.id.status);
        AwesomeTextView adddate = view.findViewById(R.id.adddate);
        Button daohang = view.findViewById(R.id.daohang);
        Button xiangqing = view.findViewById(R.id.xiangqing);

        final Taskgrid task = arrlist.get(pos);

        if(task.getTaskname().equals("")){
            taskname.setText(" ");
        }else{
            taskname.setText(task.getTaskname());
        }
//        taskname.setText(task.getTaskname());
        /*if(task.getTaskaddress().equals("")){
            taskaddress.setText(" ");
        }else{
            taskaddress.setText(task.getTaskaddress());
        }*/
//        taskaddress.setText(task.getTaskaddress());
        if(task.getAdddate().equals("")){
            adddate.setText(" ");
        }else{
            adddate.setText(task.getAdddate());
        }
//        adddate.setText(task.getAdddate());

        RequestParams params = new RequestParams(Config.SERVER_URL + "processtasks/processtasks/getListByTaskidAndPuserid");
        params.addBodyParameter("taskid", String.valueOf(task.getId()));
        params.addBodyParameter("puserid", userid);
        NetUtil.postData(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (!(result == null || result.equals("\"\""))) {
                    Gson gson = new Gson();
                    Processtask processtask = gson.fromJson(result, Processtask.class);
                    if(processtask.getStatus().equals("2")){
                        status.setText("驳回需重新上传");
                    }else if(processtask.getStatus().equals("0")){
                        status.setText("已提交任务");
                    }else if(processtask.getStatus().equals("1")){
                        status.setText("民警已采纳");
                    }
                    else if(processtask.getStatus().equals("3")){
                        status.setText("未采纳信息");
                    }

                }else{
                     if(task.getQianshou().equals("1")){
                        status.setText("已签收");
                    }else{
                         status.setText("未处理任务");
                     }

                }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {
            }
        });




        //导航
        daohang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Poi end = new Poi("", new LatLng(Double.valueOf(task.getLatitude()), Double.valueOf(task.getLongitude())),"");
                AmapNaviParams mAmapNaviParams = new AmapNaviParams(null,null,end, AmapNaviType.DRIVER);
                mAmapNaviParams.setUseInnerVoice(true);
                AmapNaviPage.getInstance().showRouteActivity(context, mAmapNaviParams, new INaviInfoCallback() {
                    @Override
                    public void onInitNaviFailure() {

                    }

                    @Override
                    public void onGetNavigationText(String s) {

                    }

                    @Override
                    public void onLocationChange(AMapNaviLocation aMapNaviLocation) {

                    }

                    @Override
                    public void onArriveDestination(boolean b) {

                    }

                    @Override
                    public void onStartNavi(int i) {

                    }

                    @Override
                    public void onCalculateRouteSuccess(int[] ints) {

                    }

                    @Override
                    public void onCalculateRouteFailure(int i) {

                    }

                    @Override
                    public void onStopSpeaking() {

                    }

                    @Override
                    public void onReCalculateRoute(int i) {

                    }

                    @Override
                    public void onExitPage(int i) {

                    }

                    @Override
                    public void onStrategyChanged(int i) {

                    }

                    @Override
                    public View getCustomNaviBottomView() {
                        return null;
                    }

                    @Override
                    public View getCustomNaviView() {
                        return null;
                    }

                    @Override
                    public void onArrivedWayPoint(int i) {

                    }
                });

            }
        });
        //详情
        xiangqing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent();
                i.putExtra("taskgrid",task);
                i.setClass(context, MytaskdetailActivity.class);
                context.startActivity(i);
            }
        });


        return view;
    }
}
